<?php
$download_pid = array();
$key = '';
while(1)
{
    download_list();
    sleep(10);
}

function download_list()
{
    global $shm_id, $key, $muAutoCaptchaPath;
    $download_list = shm_get($shm_id);
    if ($download_list == 'QUIT')
    {
//        echo("child go"); //DEBUG
        recursiveDelete('./tmp/');
        exit;
    }
    
    if(is_array($download_list)){
        foreach ($download_list as $key => $download_file)
        {
            if(($download_file['status'] == STATUS_ERROR) or ($download_file['status'] == STATUS_WAIT)) {
                $dpid = pcntl_fork();
                $download_pid[] = $dpid;
                if ($dpid == -1) {
                    shm_update($shm_id, array (
                                                'status'       => STATUS_ERROR,
                                                'status_error' => 'Fork failed'
                                                ) , $key) ;
                } else if ($dpid) {
                    shm_update($shm_id, array ( 'status' => STATUS_DOWNLOADING ), $key);
                } else {
                    $respuesta = call_user_func(pluginSelect($download_file['url']).'_download', $download_file['url'], (isset($download_file['extra']) ? $download_file['extra'] : array()));
                    if(shm_getStatus($shm_id, $key) == STATUS_CANCEL){
                        exit();
                    }elseif($respuesta['status'] == STATUS_ERROR){
                        shm_update($shm_id, array (
                                                    'status'       => STATUS_ERROR,
                                                    'status_error' => $respuesta['status_error']
                                                    ),  $key);;
                    }else{
                        if(savefile(( (isset($respuesta['filePath']) and $respuesta['filePath'])?$respuesta['filePath']:$download_file['filePath'] ), $respuesta['content'])) {
                            shm_update($shm_id, array (
                                                       'status' => STATUS_DOWNLOADED,
                                                       'status_error' => '',
                                                       'filePath'     => (isset($respuesta['filePath']) and $respuesta['filePath'])?$respuesta['filePath']:$download_file['filePath']
                                                       ) ,$key);
                        } else {
                            shm_update($shm_id, array (
                                                        'status'       => STATUS_ERROR,
                                                        'status_error' => 'Can\'t write to file'
                                                        ), $key);
                        }
                    }
                    exit();
                }
            }
        }
    }
}

function savefile($path, $data, $prefix = "dmyml_")
{
    while(file_exists('./files/'.$path)){
        $path = $prefix.$path;
    }
    return file_put_contents('./files/'.$path,$data);
}

function checkExitWhileDownloading($download_size, $downloaded, $upload_size, $uploaded)
{
    global $shm_id, $key;
    if((shm_getStatus($shm_id, $key) == STATUS_CANCEL) or (shm_get($shm_id) == 'QUIT')){
        exit();
    }
}

/**
 * Delete a file or recursively delete a directory
 *
 * @param string $str Path to file or directory
 */
function recursiveDelete($str){
    if(is_file($str)){
        return @unlink($str);
    }
    elseif(is_dir($str)){
        $scan = glob(rtrim($str,'/').'/*');
        foreach($scan as $index=>$path){
            recursiveDelete($path);
        }
        return @rmdir($str);
    }
}

?>